/**
 * Created by machan on 2017/5/10.
 */

import * as constants from '../common/Constants';
import UmfLog from '../utils/UmfLog';

class CaHelper {

    // getDataPublicKeyId() {
    //     //?
    // }
    /**
     * 获取RSA敏感数据加密公钥
     * @returns {string}
     */
    getDataPublicKey() {
        try {
            let dataPublicKey = wx.getStorageSync(constants.Keys.DATA_PUBKEY);
            if (dataPublicKey) {
                return dataPublicKey;
            }
        } catch (e) {
            UmfLog.error("缓存获取RSA敏感数据加密公钥失败");
        }
        return "-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDuMVJRnyjV64ookN/Z+Q1dZF8a9FV0v+yNkkv5btWw3RFK+mdPoidzIs2zXGf1FWjtFlYqqUkYWEKdqOdGbUEZZmD4HipJcub9JoTi2O7CFYk2NufDdU+0Z5GQE6Uw5pQgElzmV1LTQqDClD2eVzoGHmXQxOx+jpDHfsNDkTnJJQIDAQAB-----END PUBLIC KEY-----";
    }

    /**
     * 获取RSA通信加密公钥
     */
    getTxPublicKey() {
        try {
            let txPublicKey = wx.getStorageSync(constants.Keys.TX_PUBKEY);
            if (txPublicKey) {
                return txPublicKey;
            }
        } catch (e) {
            UmfLog.error("缓存获取RSA通信加密公钥失败");
        }
        return "-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDYyfEHrBtCvXkl31lGFGq7xFovIdp5fWFJJJfejzILxvvlfAM42X+lOhDKUHmcxEEZn8nExck7zAQeSSm93P79IgBFcnDpKtNNghL8idZOe77RpXFYrFTu74SLqMBGRK7f6NOGb4VbMOnX1rxTL6cqdVxc5E58CX77ICOePjTeUwIDAQAB-----END PUBLIC KEY-----";
    }

    /**
     * 更新CA证书
     * @param safeResult
     * @param isTx
     */
    updateCa(safeResult, isTx) {
        let body = safeResult.body;
        wx.setStorage({
          key: isTx ? constants.Keys.TX_PUBKEY : constants.Keys.DATA_PUBKEY,
          data: body
        });
    }

}

CaHelper = new CaHelper();
module.exports = CaHelper;